#!/usr/bin/env bash

# 编译目标品台 api 版本
ANDROID_API=21

#NDK目录
NDK_ROOT="/Users/yyh/Library/Android/sdk/ndk/21.4.7075529"
#ndk clang所在目录
TOOLCHAIN="$NDK_ROOT/toolchains/llvm/prebuilt/darwin-x86_64"

# 加入到系统路径
export PATH=$PATH:$TOOLCHAIN/bin

function build_target() {
    if [ "$1" == "armv7-a" ];
then
    # 定义存放路径
    PREFIX="$(pwd)/android/armeabi-v7a"
    TARGET_HOST="arm-linux-androideabi"
    CLANG_PREFIX="armv7a-linux-androideabi${ANDROID_API}"
elif [ "$1" == "armv8-a" ]; then
    PREFIX="$(pwd)/android/arm64-v8a"
    TARGET_HOST="aarch64-linux-android"
    CLANG_PREFIX="${TARGET_HOST}${ANDROID_API}"
elif [ "$1" == "x86" ]; then
    PREFIX="$(pwd)/android/x86"
    TARGET_HOST="i686-linux-android"
    CLANG_PREFIX="${TARGET_HOST}${ANDROID_API}"
elif [ "$1" == "x86_64" ]; then
    PREFIX="$(pwd)/android/x86_64"
    TARGET_HOST="x86_64-linux-android"
    CLANG_PREFIX="${TARGET_HOST}${ANDROID_API}"
fi

# 指定编译器 cflags 编译参数
export XCFLAGS="-isysroot ${TOOLCHAIN}/sysroot -isystem ${TOOLCHAIN}/usr/include/${TARGET_HOST} -D__ANDROID_ANDROID_API__=${ANDROID_API} -fPIC"
# 交叉编译工具，使用的是 NDK 提供的
export CROSS_COMPILE=${TOOLCHAIN}/bin/${TARGET_HOST}-

# 使用 clang 编译器
CC=${CLANG_PREFIX}-clang
AR=${TARGET_HOST}-ar
LD=${TARGET_HOST}-ld

make clean
# 安装，指定 SYS 为 android， prefix 安装路径， 不指定 CRYPTO=， 不生成共享库 SHARED ，去掉 ssl 加密
make install SYS=android prefix="$PREFIX" CC=$CC AR=$AR LD=$LD CRYPTO= SHARED= XDEF=-DNO_SSL -j8
make clean
}


for i in $* ;  do
  if [ "$i" == "all" ]; then
      echo "$i is input chart\!" ;
      build_target "armv7-a"
      build_target "armv8-a"
      build_target "x86"
      build_target "x86_64"
      else
        build_target "$i"
  fi
done
